package com.woniu.common;

import com.alibaba.fastjson.annotation.JSONField;
import com.woniu.enums.ServiceExceptionEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;


@ApiModel("接口返回结果")
@Getter
@Setter
public class ResponseInfo<T> {

    @ApiModelProperty(value = "状态码", position = 1)
    @JSONField(ordinal = 1)
    private Integer code;

    @ApiModelProperty(value = "提示", position = 2)
    @JSONField(ordinal = 2)
    private String message;

    @ApiModelProperty(value = "数据", position = 3)
    @JSONField(ordinal = 3)
    private T data;

    public static <T> ResponseInfo<T> ok() {
        return ok(null);
    }
    public static <T> ResponseInfo<T> ok(T data) {
        return responseBodyCreate(data, ServiceExceptionEnum.SUCESS);
    }

    public static <T> ResponseInfo<T> fail(String message) {
        return responseBodyCreate(ServiceExceptionEnum.FIAL.code, message);
    }

    public static <T> ResponseInfo<T> fail(Integer code, String message) {
        return responseBodyCreate(code, message);
    }

    public static <T> ResponseInfo<T> fail(ServiceExceptionEnum serviceExceptionEnum) {
        return responseBodyCreate(serviceExceptionEnum.code, serviceExceptionEnum.message);
    }

    private static <T> ResponseInfo<T> responseBodyCreate(Integer code, String message) {
        return responseBodyCreate(null, code, message);
    }

    private static <T> ResponseInfo<T> responseBodyCreate(T data, ServiceExceptionEnum serviceExceptionEnum) {
        return responseBodyCreate(data, serviceExceptionEnum.code, serviceExceptionEnum.message);
    }

    private static <T> ResponseInfo<T> responseBodyCreate(T data, int status, String message) {
        ResponseInfo<T> responseInfo = new ResponseInfo<>();
        responseInfo.setCode(status);
        responseInfo.setData(data);
        responseInfo.setMessage(message);
        return responseInfo;
    }

    public static void main(String[] args) {
        ResponseInfo.ok("sdfsdfs");
    }

}
